package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.NewsCommentDAO;
import com.wanmait.hotelManage.dao.NewsCommentReportDAO;
import com.wanmait.hotelManage.vo.NewsComment;
import com.wanmait.hotelManage.vo.NewsCommentReport;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.RoomTypeCommentReport;

/**
 * Servlet implementation class NewsCommentReportServlet
 */
@WebServlet("/manage/NewsCommentReportServlet")
public class NewsCommentReportServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("init".equals(action)) {
			this.init(request, response);
		}else if("updateNoby".equals(action)) {
			this.updateNoby(request, response);
		}else if("by".equals(action)) {
			this.by(request, response);
		}else if("updaedel".equals(action)) {
			this.updaedel(request, response);
		}else if("bys".equals(action)) {
			this.bys(request, response);
		}else if("initTwo".equals(action)) {
			this.initTwo(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("searchTwo".equals(action)) {
			this.searchTwo(request, response);
		}
	}
	/**
	 * 搜索分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void searchTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String text=request.getParameter("text");
		Integer no=Integer.parseInt(request.getParameter("id"));
		Pager pager=new Pager();
		pager.setPageNo(no);
		pager.setPageSize(4);
		int rows=NewsCommentReportDAO.getNewsCommentReportDAO().getrowss(text);
		pager.setPageCount(rows);
		List<NewsCommentReport>NewsCommentReports=NewsCommentReportDAO.getNewsCommentReportDAO().findname(text,pager);
		if(NewsCommentReports!=null) {
			List<Integer>a=new ArrayList<Integer>();
			for (NewsCommentReport NewsCommentReport : NewsCommentReports) {
				a.add(NewsCommentReport.getUserinfo().getId());
			}
			String id=a.toString();
			String id1=id.substring(1,id.length()-1);
			List<NewsComment>newsComments=NewsCommentDAO.getNewsCommentDAO().findid1(id1,pager);
			request.setAttribute("pager", pager);
			request.setAttribute("NewsCommentReports", NewsCommentReports);
			request.setAttribute("newsComments", newsComments);
			request.setAttribute("text", text);
			request.getRequestDispatcher("/manage/newsCommentReport/searchTwo.jsp").forward(request, response);
		}
	}
	/**
	 * 搜索
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String text=request.getParameter("text");
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(4);
		int rows=NewsCommentReportDAO.getNewsCommentReportDAO().getrowss(text);
		pager.setPageCount(rows);
		List<NewsCommentReport>NewsCommentReports=NewsCommentReportDAO.getNewsCommentReportDAO().findname(text,pager);
		if(NewsCommentReports!=null) {
			List<Integer>a=new ArrayList<Integer>();
			for (NewsCommentReport NewsCommentReport : NewsCommentReports) {
				a.add(NewsCommentReport.getUserinfo().getId());
			}
			String id=a.toString();
			String id1=id.substring(1,id.length()-1);
			List<NewsComment>newsComments=NewsCommentDAO.getNewsCommentDAO().findid1(id1,pager);
			request.setAttribute("pager", pager);
			request.setAttribute("NewsCommentReports", NewsCommentReports);
			request.setAttribute("newsComments", newsComments);
			request.setAttribute("text", text);
			request.getRequestDispatcher("/manage/newsCommentReport/search.jsp").forward(request, response);
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/**
	 * 页码跳转的第二个页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void initTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer no=Integer.parseInt(request.getParameter("id"));
		Pager pager=new Pager();
		pager.setPageNo(no);
		pager.setPageSize(4);
		int rows=NewsCommentReportDAO.getNewsCommentReportDAO().getrows();
		pager.setPageCount(rows);
		List<NewsCommentReport>NewsCommentReports=NewsCommentReportDAO.getNewsCommentReportDAO().findAll(pager);
		List<NewsComment>newsComments=NewsCommentDAO.getNewsCommentDAO().findAll(pager);
		request.setAttribute("pager", pager);
		request.setAttribute("NewsCommentReports", NewsCommentReports);
		request.setAttribute("newsComments", newsComments);
		request.getRequestDispatcher("/manage/newsCommentReport/initTwo.jsp").forward(request, response);
	}
	/**
	 * ajax的批量通过
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void bys(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ids=request.getParameter("ids");
		NewsCommentReportDAO.getNewsCommentReportDAO().ups(ids);
		List<Integer>newsCommentId=NewsCommentReportDAO.getNewsCommentReportDAO().findNewsCommentId(ids);
		String id=newsCommentId.toString();
		String id1=id.substring(1, id.length()-1);
//		System.out.println(id1);
		NewsCommentDAO.getNewsCommentDAO().ups(id1);
		NewsCommentReportDAO.getNewsCommentReportDAO().updateoks(ids);
	}
	/**
	 * ajax的批量退回
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updaedel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ids=request.getParameter("ids");
		NewsCommentReportDAO.getNewsCommentReportDAO().del(ids);
	}
	/**
	 * 通过
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void by(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		System.out.println(id);
		Integer id1=Integer.parseInt(request.getParameter("id1"));
		NewsCommentReportDAO.getNewsCommentReportDAO().up(id);
		NewsCommentDAO.getNewsCommentDAO().up(id1);
		NewsCommentReportDAO.getNewsCommentReportDAO().updateok(id);
		response.sendRedirect(request.getContextPath()+"/manage/NewsCommentReportServlet?action=init");
	}
	/**
	 * ajax的打回
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updateNoby(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		NewsCommentReportDAO.getNewsCommentReportDAO().de(id);
	}
	/**
	 * 初始化
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(4);
		int rows=NewsCommentReportDAO.getNewsCommentReportDAO().getrows();
		pager.setPageCount(rows);
		List<NewsCommentReport>NewsCommentReports=NewsCommentReportDAO.getNewsCommentReportDAO().findAll(pager);
		List<NewsComment>newsComments=NewsCommentDAO.getNewsCommentDAO().findAll(pager);
		request.setAttribute("pager", pager);
		request.setAttribute("NewsCommentReports", NewsCommentReports);
		request.setAttribute("newsComments", newsComments);
		request.getRequestDispatcher("/manage/newsCommentReport/list.jsp").forward(request, response);
	}
}
